﻿using System;
using Kaleida.ServiceMonitor.Framework;
using Kaleida.ServiceMonitor.Model;
using NUnit.Framework;

namespace Kaleida.UnitTests.Model
{
	[TestFixture]
	public class LimitedListTests
	{
		[Test]
		public void TestListHasMaximumCapacity()
		{
			var list = new LimitedList<string>("MyList", 4);
			Assert.AreEqual("", string.Join(", ", list.GetItemsNewestFirst()));
			list.AddToTop("cat");
			list.AddToTop("dog");
			list.AddToTop("mouse");
			list.AddToTop("rabbit");
			Assert.AreEqual("rabbit, mouse, dog, cat", string.Join(", ", list.GetItemsNewestFirst()));
			list.AddToTop("hamster");
			Assert.AreEqual("hamster, rabbit, mouse, dog", string.Join(", ", list.GetItemsNewestFirst()));
		}

		[Test]
		public void TestMaxAge()
		{
			var timeProvider = new StaticTimeProvider();

		    var list = new LimitedList<string>("MyList", 4, Duration.Parse("5 mins"));
			list.SetTimeProvider(timeProvider);
			Assert.AreEqual("", string.Join(", ", list.GetItemsNewestFirst()));
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 0, 0);
			list.AddToTop("cat");
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 0, 1);
			list.AddToTop("dog");
			list.AddToTop("mouse");

			Assert.AreEqual("mouse, dog, cat", string.Join(", ", list.GetItemsNewestFirst()));
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 4, 59);
			Assert.AreEqual("mouse, dog, cat", string.Join(", ", list.GetItemsNewestFirst()));
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 5, 00);
			Assert.AreEqual("mouse, dog, cat", string.Join(", ", list.GetItemsNewestFirst()));
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 5, 01);
			Assert.AreEqual("mouse, dog", string.Join(", ", list.GetItemsNewestFirst()));
			timeProvider.Current = new DateTime(2012, 1, 1, 0, 5, 02);
			Assert.AreEqual("", string.Join(", ", list.GetItemsNewestFirst()));
		}
	}

	public class StaticTimeProvider : ITimeProvider
	{
		public DateTime Current { get; set; }

		public DateTime GetCurrent()
		{
			return Current;
		}
	}
}
